package com.lianrui.app.goods.dao;

import java.util.List;

import org.apache.ibatis.annotations.ResultMap;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Repository;

import com.lianrui.app.goods.domain.Goods;
import com.lianrui.app.goods.vos.GoodsPage;
import com.lianrui.commons.exception.BusinessException;
import com.lianrui.core.IDao;

/**
 * 功能描述：商品数据类
 *
 * @author LJQ
 *
 *         2016年1月5日 上午10:27:07
 */
@Repository
public interface IGoodsDao extends IDao<GoodsPage, Goods, Integer> {

    @Override
    @ResultMap("goodsMap")
    @Select("select * from es_goods where goods_id = #{id}")
    public Goods unique(Integer id) throws BusinessException;

    @Select("SELECT COALESCE(SUM(num), 0) from es_order_items where goods_id =#{orderId}"
    		+ " and exists (select 1 from es_order where status != 4 and order_id = es_order_items.order_id)")
    public int getOrderItemCount(Integer orderId);

    @Select("select * from es_goods order by rand() limit #{num}")
    public List<Goods> recommendGoods(Integer num) throws BusinessException;

}
